import json
import re
from django.views import View
from ihome_pro.utils.LoginRequireJson import LoginRequiredJsonMixin
from django.http import JsonResponse
from ihome_pro.utils.ali_verify import ali_verify
from ihome_pro.utils.AliFileStorage import upload_image
from ihome_pro.utils.ImageChecking import image_file


class UserInfoView(LoginRequiredJsonMixin, View):
    '''
        获取用户信息
    '''

    def get(self, request):
        user = request.user
        try:
            avatar_url = user.avatar
        except:
            avatar_url = None
        data = {
            "avatar_url": avatar_url,
            "create_time": user.date_joined,
            "mobile": user.mobile,
            "name": user.username,
            "user_id": user.id
        }
        return JsonResponse({'errno': 0, 'errmsg': 'ok', 'data': data})


class UserAvatarView(LoginRequiredJsonMixin, View):
    '''
        上传用户头像
    '''

    def post(self, request):
        file = request.FILES.get('avatar')
        # 判断用户是上传了图片

        file_type = image_file(file)
        if not file_type:
            return JsonResponse({'errno': 4001, 'errmsg': '上传的不是图片'})
        filename = file.name
        filedata = file.read()
        # 上传头像
        try:
            image_url = upload_image(filename, filedata)
        except:
            return JsonResponse({'errno': 4001, 'errmsg': '上传头像失败'})
        # 写入头像信息
        user = request.user
        try:
            user.avatar = image_url
            user.save()
        except Exception as e:
            print(e)
            return JsonResponse({'errno': 4001, 'errmsg': '上传头像失败'})
        data = {
            'avatar_url': user.avatar
        }
        return JsonResponse({'errno': 0, 'errmsg': 'ok', 'data': data})


class ModifyUsernameView(LoginRequiredJsonMixin, View):
    '''
        修改用户名
    '''

    def put(self, request):
        recv_data = json.loads(request.body)
        new_name = recv_data.get('name')
        user = request.user
        try:
            user.username = new_name
            user.save()
        except:
            return JsonResponse({'errno': 4001, 'errmsg': '修改用户名失败'})
        return JsonResponse({'errno': 0, 'errmsg': 'ok'})


class UserAuthView(LoginRequiredJsonMixin, View):
    '''
        用户实名认证
    '''

    def post(self, request):
        recv_data = json.loads(request.body)
        real_name = recv_data.get('real_name')
        id_card = recv_data.get('id_card')
        # 验证数据
        if not all([real_name, id_card]):
            return JsonResponse({'errno': 4001, 'errmsg': '参数不完整'})
        if re.match(r'^[0-9]\d{17}[0-9xX]$', id_card):
            return JsonResponse({'errno': 4001, 'errmsg': '身份证格式错误'})
        # 调用外部接口调用身份认证
        status = ali_verify(id_card, real_name)
        if status == '01':
            user = request.user
            try:
                user.real_name = real_name
                user.id_card = id_card
                user.save()
            except:
                return JsonResponse({'errno': 4001, 'errmsg': '用户认证失败'})
        else:
            return JsonResponse({'errno': 4001, 'errmsg': '用户认证失败'})
        return JsonResponse({'errno': 0, 'errmsg': '认证成功'})

    def get(self, request):
        user = request.user
        real_name = user.real_name
        id_card = user.id_card
        data = {
            'real_name': real_name,
            'id_card': id_card
        }
        return JsonResponse({'errno': 0, 'errmsg': '认证成功', 'data': data})
